import { Prefab, SpriteFrame } from "cc";
import Singleton from "../Base/Singleton";
import { EntityTypeEnum, IActorMove, IState } from "../Common";
import { ActorManager } from "../Entity/Actor/ActorManager";
import { JoyStickManager } from "../UI/JoyStickManager";

//人物速度
const ACTOR_SPEED = 100

//数据管理类
export default class DataManager extends Singleton {
  static get Instance() {
    return super.GetInstance<DataManager>();
  }

  //摇杆
  jm: JoyStickManager

  //保存已经生成好的对象，ID是Key
  actorMap: Map<number, ActorManager> = new Map()
  //保存已经加载好的预制体
  prefabMap: Map<String, Prefab> = new Map()
  //保存已经加载好的帧动画图片
  textureMap: Map<String, SpriteFrame[]> = new Map()

  /**
   * 状态数据
   * actors   动作数组
   * id       角色id
   * position  当前坐标
   * direction  方向
   */
  state: IState = {
    actors: [
      {
        id: 1,
        type: EntityTypeEnum.Actor1,
        weaponType: EntityTypeEnum.Weapon1,
        position: {
          x: 0,
          y: 0,
        },
        direction: {
          x: 1,
          y: 0,
        }
      }
    ]
  }

  //输入方法
  applyInput(input: IActorMove) {
    const {
      id,
      dt,
      direction: { x, y },
    } = input

    //从actors数组里查找，根据ID找到需要修改的actors
    const actor = this.state.actors.find(e => e.id === id)

    //根据方向改变人物的位置
    actor.direction.x = x
    actor.direction.y = y

    //修改当前位置，方向 * 时间 * 速度
    actor.position.x += x * dt * ACTOR_SPEED
    actor.position.y += y * dt * ACTOR_SPEED
  }

}
